package com.demo.utils;

import org.springframework.util.CollectionUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author gy
 * @date 2024/4/24
 */
public class CalendarUtils2 {

    public static void main(String[] args) throws ParseException {

//        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse("2024-04-28 09:05:38");

        String currentDayStartTime = getCurrentDayStartTime(date);
        String currentDayEndTime = getCurrentDayEndTime(date);

        String currentWeekStartTime = getCurrentWeekStartTime(date);
        String currentWeekEndTime = getCurrentWeekEndTime(date);

        String currentMonthStartTime = getCurrentMonthStartTime(date);
        String currentMonthEndTime = getCurrentMonthEndTime(date);


        String lastDayStartTime = getLastDayStartTime(date);
        String lastDayEndTime = getLastDayEndTime(date);

        String lastWeekStartTime = getLastWeekStartTime(date);
        String lastWeekEndTime = getLastWeekEndTime(date);

        String lastMonthStartTime = getLastMonthStartTime(date);
        String lastMonthEndTime = getLastMonthEndTime(date);

        Integer twoDayInterval = getTwoDayInterval("2024-02-21 00:00:00", "2024-02-21 13:59:59");
        List<String> dateList = getDateListByStartAndEndDate("2023-03-01", "2023-04-01");

    }


    /**
     * 获取 本日 开始时间
     *
     * @return date
     */
    public static String getCurrentDayStartTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        return sdf.format(date);
    }

    /**
     * 获取 本日 结束时间
     *
     * @return date
     */
    public static String getCurrentDayEndTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        return sdf.format(date);
    }

    /**
     * 获取 本周 第一天（星期一） 开始时间
     *
     * @return date
     */
    public static String getCurrentWeekStartTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        if (isNowSunday(c)){
            c.add(Calendar.WEEK_OF_YEAR, -1);
        }
        // Monday 这里设置一周开始时间是星期一
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return sdf.format(c.getTime());
    }

    /**
     * 获取 本周 星期日的 结束时间
     *
     * @return date
     */
    public static String getCurrentWeekEndTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        if (isNowSunday(c)){
            c.add(Calendar.WEEK_OF_YEAR, -1);
        }
        c.add(Calendar.WEEK_OF_YEAR, 1);
        c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return sdf.format(c.getTime());
    }

    /**
     * 获取 本月 第一天（1号） 开始时间
     *
     * @return date
     */
    public static String getCurrentMonthStartTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 这里设置 一个月 开始的时间为 1号
        c.set(Calendar.DAY_OF_MONTH, 1);

        return sdf.format(c.getTime());
    }

    /**
     * 获取 本月 最后一天的 结束时间
     *
     * @return date
     */
    public static String getCurrentMonthEndTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 这里设置 一个月 开始的时间为 1号
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));

        return sdf.format(c.getTime());
    }

    /**
     * 获取 昨天 开始时间
     *
     * @return date
     */
    public static String getLastDayStartTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_YEAR, -1);
        return sdf.format(c.getTime());
    }

    /**
     * 获取 昨天 结束时间
     *
     * @return date
     */
    public static String getLastDayEndTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_YEAR, -1);
        return sdf.format(c.getTime());
    }

    /**
     * 获取 上周 第一天（星期一） 开始时间
     *
     * @return date
     */
    public static String getLastWeekStartTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        if (isNowSunday(c)){
            c.add(Calendar.WEEK_OF_YEAR, -1);
        }
        // Monday 这里设置一周开始时间是星期一
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        c.add(Calendar.WEEK_OF_YEAR, -1);
        return sdf.format(c.getTime());
    }

    /**
     * 获取 上周 星期日的 结束时间
     *
     * @return date
     */
    public static String getLastWeekEndTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        if (isNowSunday(c)){
            c.add(Calendar.WEEK_OF_YEAR, -1);
        }
        c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return sdf.format(c.getTime());
    }


    /**
     * 获取 上月 第一天（1号） 开始时间
     *
     * @return date
     */
    public static String getLastMonthStartTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 这里设置 一个月 开始的时间为 1号
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.add(Calendar.MONTH, -1);
        return sdf.format(c.getTime());
    }


    /**
     * 获取 上月 的 结束时间
     *
     * @return date
     */
    public static String getLastMonthEndTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, -1);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return sdf.format(c.getTime());
    }

    /**
     * 获取今天是否为星期日
     * @param now
     * @return
     */
    private static boolean isNowSunday(Calendar now){
        // 验证一周第一天是否为星期天
        boolean isFirstSunday = (now.getFirstDayOfWeek() == Calendar.SUNDAY);
        //获取周几
        int weekDay = now.get(Calendar.DAY_OF_WEEK);
        //若一周第一天为星期天，则-1
        if(isFirstSunday){
            weekDay = weekDay - 1;
            if(weekDay == 0){
                weekDay = 7;
            }
        }
        return weekDay == 7;
    }

    /**
     * 获取两天的时间间隔
     *
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static Integer getTwoDayInterval(String startTime, String endTime) throws ParseException {
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        //开始时间
        Date startDate = dft.parse(startTime);
        //结束时间
        Date endDate = dft.parse(endTime);
        Date nextDay = startDate;
        int i = 0;
        //当明天不在结束时间之前是终止循环
        while (nextDay.before(endDate)) {
            Calendar cld = Calendar.getInstance();
            cld.setTime(startDate);
            cld.add(Calendar.DATE, 1);
            startDate = cld.getTime();
            //获得下一天日期字符串
            nextDay = startDate;
            i++;
        }
        return i + 1;
    }

    /**
     * 通过开始时间、结束时间 获取日期列表
     *
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static List<String> getDateListByStartAndEndDate(String startDate, String endDate) throws ParseException {

        List<String> dateList = new ArrayList<>();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");

        Calendar editStartPeriod = Calendar.getInstance();
        editStartPeriod.setTime(format.parse(startDate));
        Calendar editEndPeriod = Calendar.getInstance();
        editEndPeriod.setTime(format.parse(endDate));
        Calendar curr = editStartPeriod;
        while (curr.before(editEndPeriod)) {
            String dateStr = dayFormat.format(curr.getTime());
            curr.add(Calendar.DATE, 1);
            dateList.add(dateStr);
        }
        if (!CollectionUtils.isEmpty(dateList)) {
            String finalDayStr = dayFormat.format(editEndPeriod.getTime());
            dateList.add(finalDayStr);
        }
        return dateList;
    }


}
